﻿using Abp.Authorization;
using Abp.Localization;
using Abp.MultiTenancy;
using Mt.Site.Core.Authorization;
using Mt.Site.Core.Base.Authorization;
using System.Linq;

namespace Mt.Site.Core.Taxonomys.Permissions
{
    public class TaxonomyPermissionProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreateMtPermission(AppPermissions.Pages, L("Pages"));
            var administration = pages.Children.FirstOrDefault(p => p.Name == AppPermissions.Pages_Administration)
                    ?? pages.CreateChildMtPermission(AppPermissions.Pages_Administration, L("Administration"));

            var manager = administration.CreateChildMtPermission(TaxonomyPermissions.Pages_Manage_Taxonomy, L("Taxonomy"), multiTenancySides: MultiTenancySides.Tenant|MultiTenancySides.Host);
            manager.CreateChildMtPermission(TaxonomyPermissions.Pages_Manage_Taxonomy_Create, L("CreateTaxonomy"), multiTenancySides: MultiTenancySides.Tenant|MultiTenancySides.Host);
            manager.CreateChildMtPermission(TaxonomyPermissions.Pages_Manage_Taxonomy_Update, L("UpdateTaxonomy"), multiTenancySides: MultiTenancySides.Tenant|MultiTenancySides.Host);
            manager.CreateChildMtPermission(TaxonomyPermissions.Pages_Manage_Taxonomy_Delete, L("DeleteTaxonomy"), multiTenancySides: MultiTenancySides.Tenant|MultiTenancySides.Host);
            manager.CreateChildMtPermission(TaxonomyPermissions.Pages_Manage_Taxonomy_Query, L("QueryTaxonomy"), multiTenancySides: MultiTenancySides.Tenant|MultiTenancySides.Host);
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, MtSiteConsts.LocalizationSourceName_Permissions);
        }
    }
}
